interface IAuthTree extends ng.IScope {
    commissionertList:ValueListItemResponse[];
    checkboxEvent():void;
    commissionertypeList:{ type:string[]};
    // authTreeList:{type:string[]};
    
    authTree:any,
    authNumbers:{high:number[],middle:number[],low:number[]};
    secAuthTypeTag:string[]
}


function tmsAuthTree( valueService: IValueService,$ngBootbox:BootboxService): ng.IDirective{
    return {
        templateUrl:'./src/directives/tms-authorizationTree/tms-authorizationTree.html',
        restrict: 'E',
        transclude: true,
        replace: true,
        scope : {
            authTree:"=",
            treeExpand:"=",
            authNumbers:"=",
            secAuthTypeTag:"=",
            saveChecked:"="
        },
        link  : ($scope: IAuthTree) =>{
            
            var zTree
            function getNodeListFromTree(authList){
                var nodes=[]
                authList.forEach((firstAuth,index)=>{
                    let firstNode={ id:index+1,pId:0,name:firstAuth.authorizationTypeStr,checked:false};
                    nodes.push(firstNode)
                    firstAuth.secAuthTypeList.forEach((secAuth,secIndex) => {
                        let secNode={id:(index+1)*100+secIndex+1,pId:index+1,name:secAuth.secAuthorizationType,checked:false,pNode:firstNode,};
                        nodes.push(secNode);
                        // firstNode.children.push(secNode)
                        secAuth.visualAuthList.forEach((thAuth,thIndex) => {
                            let authClass=['high','middle','low'][thAuth.level-1];
                            let thNode={
                                        id:(index+1)*10000+(secIndex+1)*100+thIndex+1,
                                        authNumber:thAuth.number, 
                                        pId:(index+1)*100+secIndex+1,
                                        pNode:secNode,
                                        gpNode:firstNode,
                                        name:thAuth.name,
                                        auth:thAuth,
                                        checked:thAuth.isShow,
                                        authClass:authClass}
                            if(thAuth.isShow){
                                secNode.checked=true;
                                firstNode.checked=true;
                            }
                            nodes.push(thNode);
                            // secNode.children.push(thNode)
                        });
                    });
                })

                return nodes;
            }
           
            var setting = {
                view: {
                    selectedMulti: false
                },
                check: {
                    enable: true
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                edit: {
                    enable: false
                },
                callback:{
                    onCheck:function(e,treeId,treeNode){
                        
                        let firstName=null,secNames=[],thNames=[]
                        switch(treeNode.level){
                            case 0:{
                                firstName=treeNode.name;
                                treeNode.children.forEach(secNode=>{
                                    secNames.push(secNode.name);
                                    secNode.children.forEach(thNode=>{
                                        thNames.push(thNode.name)
                                    })
                                })
                            };break;
                            case 1:{
                                firstName=treeNode.pNode.name;
                                secNames.push(treeNode.name);
                                treeNode.children.forEach(thNode=>{
                                    thNames.push(thNode.name)
                                })
                            };break;
                            case 2:{
                                thNames.push(treeNode.name);
                                secNames.push(treeNode.pNode.name);
                                firstName=treeNode.gpNode.name
                            }
                        };
                        // console.log(firstName,secNames,thNames);
                        $scope.authTree.data.forEach(firstAuth=>{
                            (firstAuth.authorizationTypeStr===firstName)&&firstAuth.secAuthTypeList.forEach(secAuth=>{
                                (secNames.indexOf(secAuth.secAuthorizationType)>-1)&&secAuth.visualAuthList.forEach(thAuth=>{
                                    (thNames.indexOf(thAuth.name)>-1)&&(thAuth.isShow=treeNode.checked);
                                })
                            })
                        })
                        // console.log($scope.authTree);
                    }
                }
            };
            var zNodes =[];
            
            $scope.$watch('authTree',function(newValue,oldValue){
                if(newValue&&newValue['data']){
                   
                    zNodes=getNodeListFromTree(newValue['data']);
                    // console.log(zNodes)
                    $.fn.zTree.destroy("authTree");
                    $.fn.zTree.init($("#authTree"), setting, zNodes);
                }
               
            })
            $scope.$watch('treeExpand',function(newValue,oldValue){

                zTree = $.fn.zTree.getZTreeObj("authTree");
                zTree&&zTree.expandAll(newValue); 
                
                
            });
            

            $scope.$watch('saveChecked',function(newValue,oldValue){
                // console.log(newValue)
                zTree = $.fn.zTree.getZTreeObj("authTree");
                let checkedNumbers={high:[],middle:[],low:[]},secAuthTags=[];
                if(zTree){
                    let nodes=zTree.getNodes();
                    // console.log(nodes)
                    nodes&&nodes.forEach(firstNode=>{
                        firstNode.children&&firstNode.children.forEach(secNode=>{
                            secNode.children&&secNode.children.forEach(thNode=>{
                                if(thNode.checked){
                                    // checkedNumbers.push(thNode.authNumber);
                                    let key=thNode.authClass;
                                    if(key){

                                        // console.log(checkedNumbers);
                                        checkedNumbers[key].push(thNode.authNumber);
                                    }
                                    
                                    if(secAuthTags.indexOf(thNode.auth.secAuthorizationTypeTag)===-1){
                                        secAuthTags.push(thNode.auth.secAuthorizationTypeTag);
                                    }
                                }
                            })
                        })
                        
                    })
                    
                    
                }
                $scope.authNumbers=checkedNumbers;
                $scope.secAuthTypeTag=secAuthTags;
                
             
            })
            
    
            

        }
    };
}


tmsAuthTree['$inject'] = ["valueService","$ngBootbox"]
angular.module('tmsAuthTree', []).directive('tmsAuthTree', tmsAuthTree);






